﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProyectoAlmacen.Models;
using ProyectoAlmacen.Negocio;

namespace ProyectoAlmacen.Controllers
{
    public class MaterialController : Controller
    {
        public static MaterialServiceImpl materialService { get; set; }
        

        public ActionResult Index()
        {

            materialService = new MaterialServiceImpl();
            ICollection<Material> model = materialService.ListarMaterial();
            return View(model);
        }

        //
        // GET: /Material/Details/5

        public ActionResult Details(int id)
        {
            Material model = materialService.ObtenerMaterial(id);
            return View(model);
        }

        //
        // GET: /Material/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Material/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                int idMaterial = int.Parse(collection["idMaterial"]);
                string Descripcion = collection["Descripcion"];
                int Estado = int.Parse(collection["Estado"]);
                int idUsuario = int.Parse(collection["idUsuario"]);
                materialService.CrearMaterial(idMaterial,Descripcion,Estado,idUsuario);
                

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Material/Edit/5
 
        public ActionResult Edit(int id)
        {
            Material model = materialService.ObtenerMaterial(id);
            return View(model);
        }

        //
        // POST: /Material/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                Material model = materialService.ObtenerMaterial(id);
                model.idMaterial = int.Parse(collection["idMaterial"]);
                model.Descripcion = collection["Descripcion"];
                model.Estado = int.Parse(collection["Estado"]);
                model.idUsuario = int.Parse(collection["idUsuario"]);
                materialService.EditarMaterial(model.idMaterial, model.Descripcion, model.Estado, model.idUsuario);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Material/Delete/5
 
        public ActionResult Delete(int id)
        {
            Material model = materialService.ObtenerMaterial(id);
            return View(model);
        }

        //
        // POST: /Material/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                List<Material> materiales = (List<Material>)Session["materiales"];
                materiales.Remove(materialService.ObtenerMaterial(id));
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
